<?php
class Gallery_Model_Album extends Promotor_Model_Scaffold {
	
	protected $_dbTableClass = 'Gallery_Model_DbTable_Album';

	protected $_cachedMethods = array('fetchAllById','fetchAllByAlias');
	
	/**
	 * @return Zend_Db_Select
	 */
	public function selectList() {
		$table = $this->getDbTable();
		$select = new Zend_Db_Select($table->getAdapter());
		$select->from(array('ga' => 'gallery_album'))
			   ->joinLeft(array('g'=>'gallery'),'g.id = ga.gallery_id', array('gallery_name' => 'name'));
		return $select;
	}
	
	/**
	 * @param integer $id
	 * @return array
	 */
	public function fetchAllById($id) {
		$table = $this->getDbTable();
		$adapter = $table->getAdapter();

		$select = $table->select()
			->where('id = ?', $id)
			->where('publicated = 1');

		try {
			$row = $table->fetchRow($select);
		} catch (Zend_Db_Table_Exception $e) {
			$this->_addException($e);
			$this->_setStatus(self::FAILURE);
		}
		
		if (!$row instanceof Zend_Db_Table_Row_Abstract) {
			$this->_addMessage('row not instance of "Zend_Db_Table_Row_Abstract"');
			$this->_setStatus(self::FAILURE);
			return;
		}

		$select1 = new Zend_Db_Select($adapter);
		$select1->from(
					array('gallery_media'),
					array(
						'id',
						'name',
						'description',
						'image',
						'media',
						'type' => new Zend_Db_Expr("'media'"),
						'width' => new Zend_Db_Expr('NULL'),
						'height' => new Zend_Db_Expr('NULL'),
						'idx'))
				 ->where('publicated = ?', 1)
			 	->where('album_id = ?', $row->id);

		$select2 = new Zend_Db_Select($adapter);
		$select2->from(
					array('gallery_image'),
					array(
						'id',
						'name',
						'description',
						'image',
						'media' => new Zend_Db_Expr('NULL'),
						'type' => new Zend_Db_Expr("'image'"),
						'width',
						'height',
						'idx'))
			 	->where('publicated = ?', 1)
			 	->where('album_id = ?', $row->id);

		$select = new Zend_Db_Select($adapter);
		$select->union(array($select1, $select2))
			   ->order('idx ASC');

		try {
			$stmt = $select->query();
			$rowset = $stmt->fetchAll();
			$this->_setStatus(self::SUCCESS);
			return array($row->toArray(), $rowset);
		} catch(Zend_Db_Exception $e) {
			$this->_addException($e);
			$this->_setStatus(self::FAILURE);
		}
	}
	
	/**
	 * @param integer $id
	 * @return array
	 */
	public function fetchAllByAlias($alias) {
		$table = $this->getDbTable();
		$adapter = $table->getAdapter();

		$select = $table->select()
			->where('alias = ?', $alias)
			->where('publicated = 1');

		try {
			$row = $table->fetchRow($select);
		} catch (Zend_Db_Table_Exception $e) {
			$this->_addException($e);
			$this->_setStatus(self::FAILURE);
		}
		
		if (!$row instanceof Zend_Db_Table_Row_Abstract) {
			$this->_addMessage('row not instance of "Zend_Db_Table_Row_Abstract"');
			$this->_setStatus(self::FAILURE);
			return;
		}

		$select1 = new Zend_Db_Select($adapter);
		$select1->from(
					array('gallery_media'),
					array(
						'id',
						'name',
						'description',
						'image',
						'media',
						'type' => new Zend_Db_Expr("'media'"),
						'width' => new Zend_Db_Expr('NULL'),
						'height' => new Zend_Db_Expr('NULL'),
						'idx'))
				 ->where('publicated = ?', 1)
			 	->where('album_id = ?', $row->id);

		$select2 = new Zend_Db_Select($adapter);
		$select2->from(
					array('gallery_image'),
					array(
						'id',
						'name',
						'description',
						'image',
						'media' => new Zend_Db_Expr('NULL'),
						'type' => new Zend_Db_Expr("'image'"),
						'width',
						'height',
						'idx'))
			 	->where('publicated = ?', 1)
			 	->where('album_id = ?', $row->id);

		$select = new Zend_Db_Select($adapter);
		$select->union(array($select1, $select2))
			   ->order('idx ASC');

		try {
			$stmt = $select->query();
			$rowset = $stmt->fetchAll();
			$this->_setStatus(self::SUCCESS);
			return array($row->toArray(), $rowset);
		} catch(Zend_Db_Exception $e) {
			$this->_addException($e);
			$this->_setStatus(self::FAILURE);
		}
	}
	
	/**
	 * @param integer $id
	 * @return array
	 */
	public function fetchAllGruperByGallery() {
		$table = $this->getDbTable();
		$adapter = $table->getAdapter();
		$select = new Zend_Db_Select($adapter);
		$select->from(array('g' => 'gallery'),array('gallery_name'=>'name','gallery_key'=>'id'))
			   ->join(array('ga' => 'gallery_album'), 'g.id = ga.gallery_id', array('key' => 'id','value' =>'name'));

		try {
			$stmt = $select->query();
		} catch (Zend_Db_Statement_Exception $e) {
			$this->_setStatus(self::FAILURE);
			$this->_addException($e);
			return;
		}

		$result = array();
		while ($data = $stmt->fetch()) {
			$key = $data['gallery_name'];
			unset($data['gallery_name']);
			if (!array_key_exists($key, $result)) {
				$result[$key] = array();
			}
			
			if (!array_key_exists($data['key'], $result[$key])) {
				$result[$key][$data['key']] = $data['value'];
			}
		}
		return $result;
	}
	
	/**
	 * @return array
	 */
	public function fetchAll() {
		$table = $this->getDbTable();
		try {
			$rowset = $table->fetchAll();
			$this->_setStatus(self::SUCCESS);
			return $rowset->toArray();
		} catch (Zend_Db_Table_Exception $e) {
			$this->_setStatus(self::FAILURE);
			$this->_addException($e);
		}
	}
	
	/**
	 * @param integer $id
	 * @param bool $asArray
	 * @return array
	 */
	public function findById($id, $asArray = true) {
		$table = $this->getDbTable();
		try {
			$rowset = $table->find((int) $id);
			if(!count($rowset)) {
				$this->_setStatus(self::FAILURE);
				return;
			}
			$this->_setStatus(self::SUCCESS);
			$current = $rowset->current();
			return $asArray
				? $current->toArray()
				: $current;
		} catch (Zend_Db_Table_Exception $e) {
			$this->_setStatus(self::FAILURE);
			$this->_addException($e);
		}
	}
	
	/**
	 * @param string $alias
	 * @param bool $asArray
	 * @return array
	 */
	public function findByAlias($alias, $asArray = true) {
		$table = $this->getDbTable();
		$where = $table->getAdapter()->quoteInto('alias = ?', $alias);

		try {
			$row = $table->fetchRow($where);
			if(!$row instanceof Zend_Db_Table_Row_Abstract) {
				$this->_setStatus(self::FAILURE);
				return;
			}
			$this->_setStatus(self::SUCCESS);
			return $asArray
				? $row->toArray()
				: $row;
		} catch (Zend_Db_Table_Exception $e) {
			$this->_setStatus(self::FAILURE);
			$this->_addException($e);
		}
	}
	
	/**
	 * @param integer $newsId
	 * @return array
	 */
	public function getAttachedSitesId($albumId) {
		$table = $this->getDbTable();
		$select = $table->select()->where('id = ?', $albumId);
		try {
			$row = $table->fetchRow($select);
		} catch (Zend_Db_Exception $e) {
			$this->_addException($e);
			$this->_setStatus(self::FAILURE);
			return;
		}
		
		if (!$row instanceof Zend_Db_Table_Row_Abstract) {
			$this->_setStatus(self::FAILURE);
			return;
		}

		try {
			$rowset = $row->findDependentRowset('Site_Model_DbTable_HasAlbum','Album');
		} catch (Zend_Db_Exception $e) {
			$this->_addException($e);
			$this->_setStatus(self::FAILURE);
			return;
		}
		
		$return = array();
		foreach ($rowset as $row) {
			$return[] = $row->site_id;
		}
		
		$this->_setStatus(self::SUCCESS);
		return $return;
	}
}